﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataEntity;
using System.Data.Linq;
using System.Diagnostics;
using DataEntity.DTO;
namespace DataAccess
{
    public partial class DataProvider
    {
        //Logger.TimeLog objLogger;
        #region LoginUser
        public LoginUser ValidateUser(string email, string password)
        {
            using (PropteaDataContext db = new PropteaDataContext())
            {
                db.ObjectTrackingEnabled = false;
                db.DeferredLoadingEnabled = false;
                if (SettingProvider.IsLoggerEnable()) { StackTrace st = new StackTrace(new StackFrame(true)); Console.WriteLine(" Stack trace for current level: {0}", st.ToString()); StackFrame sf = st.GetFrame(0); string FunctionData = ""; FunctionData += string.Format(" File: {0}", sf.GetFileName()); FunctionData += string.Format(" Method: {0}", sf.GetMethod().Name); FunctionData += string.Format(" Line Number: {0}", sf.GetFileLineNumber()); FunctionData += string.Format(" Column Number: {0}", sf.GetFileColumnNumber()); objLogger = new Logger.TimeLog(FunctionData); }
                db.ObjectTrackingEnabled = false;
                var data = (from p in db.LoginUsers where p.Username.ToLower().Trim() == email.ToLower().Trim() && p.Password == password select p).ToList();

                if (SettingProvider.IsLoggerEnable())
                {
                    objLogger.StopTime();
                }
                if (data.Count > 0)
                {
                    return data[0];
                }
                else
                {
                    return null;
                }

            }
        }

      
        #endregion

        #region ContactDetails
        public void ContactDetailAdd(Guid LoginID, string Mobile)
        {
            using (PropteaDataContext db = new PropteaDataContext())
            {

                DataLoadOptions option = new DataLoadOptions();
                db.LoadOptions = option;

                var contactdata = ContactDetailGetbyLoginID(LoginID);

                ContactDetail ObjContactDetail = new ContactDetail();

                if (contactdata.Count == 0)
                {
                    ContactDetailAdd(Guid.NewGuid(), LoginID, Mobile, "", DateTime.Now);
                }
                else
                {
                    bool isUpdated = false;
                    foreach (ContactDetail cd in contactdata)
                    {
                        if (cd.Mobile == null)
                        {
                            isUpdated = true;
                            ContactDetailUpdateByContactDetailID(cd.ContactDetailID, cd.LoginID, Mobile, cd.LandLine, DateTime.Now);
                        }
                    }
                    if (!isUpdated)
                    {
                        ContactDetailAdd(Guid.NewGuid(), LoginID, Mobile, "", DateTime.Now);
                    }
                }
                db.SubmitChanges();
                if (SettingProvider.IsLoggerEnable()) { objLogger.StopTime(); }
            }
        }
        #endregion

        public void PropertyAdd(Property property, Address address, AreaDetail areaDetail, TransactionDetail transactionDetail, PropertyDescription propertyDescription)
        {
            using (PropteaDataContext db = new PropteaDataContext())
            {

                if (SettingProvider.IsLoggerEnable()) { StackTrace st = new StackTrace(new StackFrame(true)); Console.WriteLine(" Stack trace for current level: {0}", st.ToString()); StackFrame sf = st.GetFrame(0); string FunctionData = ""; FunctionData += string.Format(" File: {0}", sf.GetFileName()); FunctionData += string.Format(" Method: {0}", sf.GetMethod().Name); FunctionData += string.Format(" Line Number: {0}", sf.GetFileLineNumber()); FunctionData += string.Format(" Column Number: {0}", sf.GetFileColumnNumber()); objLogger = new Logger.TimeLog(FunctionData); }

                DataLoadOptions option = new DataLoadOptions();
                db.LoadOptions = option;
                db.DeferredLoadingEnabled = false;

                Address ObjAddress = new Address();
                ObjAddress = address;
                db.Addresses.InsertOnSubmit(ObjAddress);

                AreaDetail ObjAreaDetail = new AreaDetail();
                ObjAreaDetail = areaDetail;
                db.AreaDetails.InsertOnSubmit(ObjAreaDetail);

                TransactionDetail ObjTransactionDetail = new TransactionDetail();
                ObjTransactionDetail = transactionDetail;
                db.TransactionDetails.InsertOnSubmit(ObjTransactionDetail);

                PropertyDescription objPropertyDescription = new PropertyDescription();
                objPropertyDescription = propertyDescription;
                db.PropertyDescriptions.InsertOnSubmit(objPropertyDescription);

                Property objProperty = new Property();
                objProperty = property;
                db.Properties.InsertOnSubmit(property);

                db.SubmitChanges();

                if (SettingProvider.IsLoggerEnable()) { objLogger.StopTime(); }
            }
        }

        public void UpdatePropertywithAdditionalData(Guid propertyID, PropertyAditionalFeature propertyAditionalFeature, List<Guid> featureIDs, ProximityLandmark proximityLandmark)
        {
            using (PropteaDataContext db = new PropteaDataContext())
            {

                if (SettingProvider.IsLoggerEnable()) { StackTrace st = new StackTrace(new StackFrame(true)); Console.WriteLine(" Stack trace for current level: {0}", st.ToString()); StackFrame sf = st.GetFrame(0); string FunctionData = ""; FunctionData += string.Format(" File: {0}", sf.GetFileName()); FunctionData += string.Format(" Method: {0}", sf.GetMethod().Name); FunctionData += string.Format(" Line Number: {0}", sf.GetFileLineNumber()); FunctionData += string.Format(" Column Number: {0}", sf.GetFileColumnNumber()); objLogger = new Logger.TimeLog(FunctionData); }

                DataLoadOptions option = new DataLoadOptions();
                db.LoadOptions = option;
                db.DeferredLoadingEnabled = false;
                PropertyAditionalFeature objpropertyAditionalFeature = new PropertyAditionalFeature();
                objpropertyAditionalFeature = propertyAditionalFeature;
                db.PropertyAditionalFeatures.InsertOnSubmit(objpropertyAditionalFeature);

                PropertyAmenity propertyAmenity = new PropertyAmenity();
                propertyAmenity.ModifiedDate = DateTime.Now;
                propertyAmenity.PropertyAmenityID = Guid.NewGuid();
                propertyAmenity.ProximityLandmark = proximityLandmark;

                PropertyFeature propertyFeature = new PropertyFeature();
                foreach (Guid featureId in featureIDs)
                {
                    propertyFeature.FeatureID = featureId;
                    propertyFeature.ModifiedDate = DateTime.Now;
                    propertyFeature.PropertyAmenityID = propertyAmenity.PropertyAmenityID;
                    propertyFeature.PropertyFeatureID = Guid.NewGuid();
                }

                Property data = db.Properties.Single(p => p.PropertyID == propertyID);
                data.ModifiedDate = DateTime.Now;
                data.PropertyAditionalFeatureID = objpropertyAditionalFeature.PropertyAditionalFeatureID;
                data.PropertyAmenityID = propertyAmenity.PropertyAmenityID;

                db.SubmitChanges();
                if (SettingProvider.IsLoggerEnable()) { objLogger.StopTime(); }

            }
        }

        //public void PropertyAdd(PropertyAddStep1DTO propertyAddStep1DTO, Address address, AreaDetail areaDetail, TransactionDetail transactionDetail, PropertyDescription propertyDescription)
        //{
        //    using (PropteaDataContext db = new PropteaDataContext())
        //    {
        //        db.ObjectTrackingEnabled = false;
        //        db.DeferredLoadingEnabled = false;
        //        if (SettingProvider.IsLoggerEnable()) { StackTrace st = new StackTrace(new StackFrame(true)); Console.WriteLine(" Stack trace for current level: {0}", st.ToString()); StackFrame sf = st.GetFrame(0); string FunctionData = ""; FunctionData += string.Format(" File: {0}", sf.GetFileName()); FunctionData += string.Format(" Method: {0}", sf.GetMethod().Name); FunctionData += string.Format(" Line Number: {0}", sf.GetFileLineNumber()); FunctionData += string.Format(" Column Number: {0}", sf.GetFileColumnNumber()); objLogger = new Logger.TimeLog(FunctionData); }
        //        db.ObjectTrackingEnabled = false;
        //        Property property = new Property();
        //        AddressAdd(address);
        //        property.AddressID = address.AddressID;
        //        AreaDetailAdd(areaDetail);
        //        property.AreaDetailID = areaDetail.AreaDetailID;
        //        TransactionDetailAdd(transactionDetail);
        //        property.TransactionDetailID = transactionDetail.TransactionDetailID;
        //        property.PropertyDescription = propertyDescription;
        //        property.Bathroom = propertyAddStep1DTO.Bathroom;
        //        property.Bedroom = propertyAddStep1DTO.Bedroom;
        //        property.ModifiedDate = DateTime.Now;
        //        property.PropertyID = Guid.NewGuid();
        //        property.QualityRatting = propertyAddStep1DTO.QualityRatting.ToString();
        //        property.TotalRoom = propertyAddStep1DTO.TotalRoom;
        //        property.WashRoom = propertyAddStep1DTO.Washroom;
        //        db.Properties.InsertOnSubmit(property);
        //        db.SubmitChanges();
        //        if (SettingProvider.IsLoggerEnable())
        //        {
        //            objLogger.StopTime();
        //        }
        //    }
        //}

        //public void PropertyAdd(PropertyAddStep1DTO propertyAddStep1DTO, Address address, AreaDetail areaDetail, TransactionDetail transactionDetail, PropertyDescription propertyDescription, PropertyAditionalFeature propertyAditionalFeature, List<Guid> features, ProximityLandmark proximityLandmark)
        //{
        //    using (PropteaDataContext db = new PropteaDataContext())
        //    {
        //        db.ObjectTrackingEnabled = false;
        //        db.DeferredLoadingEnabled = false;
        //        if (SettingProvider.IsLoggerEnable()) { StackTrace st = new StackTrace(new StackFrame(true)); Console.WriteLine(" Stack trace for current level: {0}", st.ToString()); StackFrame sf = st.GetFrame(0); string FunctionData = ""; FunctionData += string.Format(" File: {0}", sf.GetFileName()); FunctionData += string.Format(" Method: {0}", sf.GetMethod().Name); FunctionData += string.Format(" Line Number: {0}", sf.GetFileLineNumber()); FunctionData += string.Format(" Column Number: {0}", sf.GetFileColumnNumber()); objLogger = new Logger.TimeLog(FunctionData); }
        //        db.ObjectTrackingEnabled = false;
        //        Property property = new Property();
        //        property.Address = address;
        //        property.AreaDetail = areaDetail;
        //        property.TransactionDetail = transactionDetail;
        //        property.PropertyDescription = propertyDescription;
        //        property.Bathroom = propertyAddStep1DTO.Bathroom;
        //        property.Bedroom = propertyAddStep1DTO.Bedroom;
        //        property.ModifiedDate = DateTime.Now;
        //        property.PropertyID = Guid.NewGuid();
        //        property.QualityRatting = propertyAddStep1DTO.QualityRatting.ToString();
        //        property.TotalRoom = propertyAddStep1DTO.TotalRoom;
        //        property.WashRoom = propertyAddStep1DTO.Washroom;
        //        property.PropertyAditionalFeature = propertyAditionalFeature;
        //        PropertyAmenity propertyAmenity = new PropertyAmenity();
        //        propertyAmenity.ModifiedDate = DateTime.Now;
        //        propertyAmenity.PropertyAmenityID = Guid.NewGuid();
        //        propertyAmenity.ProximityLandmark = proximityLandmark;
        //        foreach (Guid featureID in features)
        //        {
        //            PropertyFeature pf = new PropertyFeature();
        //            pf.FeatureID = featureID;
        //            pf.ModifiedDate = DateTime.Now;
        //            pf.PropertyAmenityID = propertyAmenity.PropertyAmenityID;
        //            pf.PropertyFeatureID = Guid.NewGuid();
        //            propertyAmenity.PropertyFeatures.Add(pf);
        //        }
        //        property.PropertyAmenity = propertyAmenity;
        //        property.

        //        db.Properties.InsertOnSubmit(property);
        //        db.SubmitChanges();
        //        if (SettingProvider.IsLoggerEnable())
        //        {
        //            objLogger.StopTime();
        //        }
        //    }
        //}

        #region PropertyTypeControlSetting
        public List<PropertyTypeControlSetting> PropertyTypeControlSettingGetbyPropertyTypeID(int propertyTypeID, string ControlClientID)
        {
            if (SettingProvider.IsLoggerEnable()) { StackTrace st = new StackTrace(new StackFrame(true)); Console.WriteLine(" Stack trace for current level: {0}", st.ToString()); StackFrame sf = st.GetFrame(0); string FunctionData = ""; FunctionData += string.Format(" File: {0}", sf.GetFileName()); FunctionData += string.Format(" Method: {0}", sf.GetMethod().Name); FunctionData += string.Format(" Line Number: {0}", sf.GetFileLineNumber()); FunctionData += string.Format(" Column Number: {0}", sf.GetFileColumnNumber()); objLogger = new Logger.TimeLog(FunctionData); }
            using (PropteaDataContext db = new PropteaDataContext())
            {
                db.ObjectTrackingEnabled = false;
                db.DeferredLoadingEnabled = false;
                var data = (from p in db.PropertyTypeControlSettings where p.ControlClientID == ControlClientID && p.PropertyTypeID == propertyTypeID select p).ToList();
                if (SettingProvider.IsLoggerEnable()) { objLogger.StopTime(); }
                return data;
            }
        }
        #endregion
    }
}
